//
//  ReweetedStatusView.swift
//  SinaWeibo
//
//  Created by yang on 15/11/16.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit
import SnapKit
import FFLabel
class ReweetedStatusView: UIView {
    var bottomConstraints:Constraint?
    var retweetedStatus:Status?{
        didSet{
            retweetedLabel.text = retweetedStatus?.text
            //移除约束
            self.bottomConstraints?.uninstall()
            if let picture = retweetedStatus?.urlImages where picture.count > 0{
                pictureView.imageURLs = picture
                pictureView.backgroundColor = UIColor(white: 0.95, alpha: 1)
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(retweetedLabel).offset(StatusCellMargin).constraint
            })
                
         }

        }
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        retweetedLabel.labelDelegate = self;
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        addSubview(retweetedLabel)
        addSubview(pictureView)
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
         self.bottomConstraints =
            make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
        }
        
    }
    private lazy var retweetedLabel:FFLabel = FFLabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    private lazy var  pictureView:StatusPictureView = StatusPictureView()
}
extension ReweetedStatusView:FFLabelDelegate{
    func labelDidSelectedLinkText(label: FFLabel, text: String){
        if text.hasPrefix("http") {
            let web = HomeWebViewController()
            web.urlString = text
            self.NavController()?.pushViewController(web, animated: true)
        }
    }
    
}